package com.meizi.daily.biz;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meizi.core.tool.api.PageResult;
import com.meizi.core.tool.api.R;
import com.meizi.daily.entity.Category;
import com.meizi.daily.mapper.CategoryMapper;
import com.meizi.daily.vo.CategoryHierarchyVo;
import com.meizi.daily.vo.CategoryMenuVo;
import com.meizi.core.tool.api.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author meizi
 * @email enjoymeizi@gmail.com
 * @date 2020-05-26 17:24:23
 */
@Service
public class CategoryBiz {
    @Autowired
    private CategoryMapper mapper;

    public List<CategoryMenuVo> getMenuList() {
        return mapper.getMenuList();
    }

    public R hierarchy(PageParam pageInfo) {
        //查询分页
        Category category = new Category();
        category.setLevel("1");
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<Category> categories = select(category);
        PageInfo<Category> of = PageInfo.of(categories);
        long total = of.getTotal();
        List<Integer> collect = categories.stream().map(Category::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return new R().ofSuccess();
        }
        List<CategoryHierarchyVo> result = mapper.getHierarchy(collect);
        return new R().ofSuccess(PageResult.of(result, total));
    }

    public List<Category> select(Category category) {
        return mapper.select(category);
    }

    public void addCategory(Category category) {
        mapper.insert(category);
    }

    public void deleteById(int id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void updateSelectiveById(Category entity) {
        mapper.updateByPrimaryKey(entity);
    }
}
